﻿using System;
using System.Collections.Generic;
using XCI.Core;
using XCI.Sys.Model;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统参数服务
    /// </summary>
    public class ParamService : BaseService<SysParam>
    {
        /// <summary>
        /// 默认实例
        /// </summary>
        public static readonly ParamService Instance = new ParamService();

        /// <summary>
        /// 接口根路径前缀
        /// </summary>
        protected override string RootUrl => "/api/sys/param/";

        /// <summary>
        /// 新建参数对象
        /// </summary>
        public SysParam New()
        {
            return new SysParam
            {
                Id = NewId()
            };
        }

        /// <summary>
        /// 复制参数对象
        /// </summary>
        /// <param name="original">原对象</param>
        public SysParam Copy(SysParam original)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            var model = original?.Clone();
            model.Id = NewId();
            return model;
        }

        /*
        /// <summary>
        /// 检查参数编码是否存在
        /// </summary>
        /// <param name="code">参数编码</param>
        public bool ExistByCode(string code)
        {
            return ExistByCodeCore(code);
        }

        /// <summary>
        /// 检查参数名称是否存在
        /// </summary>
        /// <param name="name">参数名称</param>
        public bool ExistByName(string name)
        {
            return ExistByNameCore(name);
        }
        */

        /// <summary>
        /// 新建参数
        /// </summary>
        /// <param name="model">参数对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysParam model)
        {
            return InsertCore(model);
        }

        /// <summary>
        /// 修改参数
        /// </summary>
        /// <param name="model">参数对象</param>
        /// <returns>成功返回true</returns>
        public bool Update(SysParam model)
        {
            return UpdateCore(model);
        }

        /*
        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="id">参数主键</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string id)
        {
            return DeleteByIdCore(id);
        }
        */

        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="ids">主键字符串,多个主键之间逗号隔开</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string ids)
        {
            return DeleteByIdsCore(ids);
        }

        /// <summary>
        /// 根据主键获取参数对象
        /// </summary>
        /// <param name="id">参数主键</param>
        public SysParam SelectById(string id)
        {
            return SelectByIdCore(id);
        }

        /// <summary>
        /// 根据参数编码获取参数对象
        /// </summary>
        /// <param name="code">参数编码</param>
        /// <returns>成功返回参数对象</returns>
        public SysParam SelectByCode(string code)
        {
            return SelectByCodeCore(code);
        }

        /// <summary>
        /// 根据编码查询单个参数值
        /// </summary>
        /// <param name="code">参数编码</param>
        /// <param name="defaultValue">如果没有找到指定编码的参数,则返回此默认值</param>
        /// <returns>返回指定编码的参数值</returns>
        public string SelectValueByCode(string code, string defaultValue)
        {
            var url = GetUrl("selectValueByCode");
            var map = Map.New.Add("code", code).Add("defaultValue", defaultValue);
            return SysRuntime.Api.Post<string>(url, map).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询参数分页列表
        /// </summary>
        /// <param name="paged">分页参数对象</param>
        /// <param name="key">编码/名称关键字</param>
        public PageList<SysParam> SelectPageList(PageParam paged, string key)
        {
            var map = ToPageMap(paged).Add(SysConst.FKeyName, key);
            return SelectPageListCore(map);
        }

        /// <summary>
        /// 查询参数列表
        /// </summary>
        /// <param name="key">编码/名称关键字</param>
        public List<SysParam> SelectList(string key)
        {
            var map = Map.New.Add(SysConst.FKeyName, key);
            return SelectListCore(map);
        }

        /// <summary>
        /// 导出参数列表
        /// </summary>
        /// <param name="key">编码/名称关键字</param>
        public byte[] Export(string key)
        {
            var map = Map.New.Add(SysConst.FKeyName, key);
            return ExportCore(map);
        }

        /// <summary>
        /// 刷新服务端参数缓存
        /// </summary>
        public bool RefreshCache()
        {
            return RefreshCacheCore();
        }
    }
}
